package com.example.startapp;
//商户登陆注册功能



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ShangHuActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private EditText etShopName;
    private Button btnRegister;
    private Button btnLogin;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shang_hu);

        etUsername=findViewById(R.id.shopN);
        etPassword=findViewById(R.id.shopPsw);
        etShopName=findViewById(R.id.shopName);
        btnLogin=findViewById(R.id.login_shanghu);
        btnRegister=findViewById(R.id.register_shanghu);


        btnRegister.setOnClickListener(v->registerMerchant());

        btnLogin.setOnClickListener(v->loginMerchant());


    }

    private void registerMerchant()
    {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String shopName = etShopName.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(shopName)) {
            Toast.makeText(ShangHuActivity.this, "请输入用户名、密码和店铺名", Toast.LENGTH_LONG).show();
            return;
        }

        OkHttpClient client = new OkHttpClient(); // 创建http客户端
        String json = String.format("{\n" +
                "   \"userName\":\"%s\",\n" +
                "   \"userPwd\":\"%s\",\n" +
                "   \"shopName\":\"%s\"\n" +
                "}", username, password, shopName);
        String url = "http://192.168.239.136:8888/api/merchant/register"; // 假设商家注册的接口地址是这个
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build(); // 创建http请求

        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "注册失败1: " + e.getMessage(), Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "注册失败2: " + response.code(), Toast.LENGTH_SHORT).show());
                    return;
                }
                String responseBody = response.body().string();
                if (responseBody.isEmpty()) {
                    runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "注册失败: 服务器返回空响应", Toast.LENGTH_SHORT).show());
                } else {
                    runOnUiThread(() -> {
                        Toast.makeText(ShangHuActivity.this, "注册成功！请重新登录", Toast.LENGTH_LONG).show();
                        finish();
                    });
                }
            }
        });
    }

    private void loginMerchant()
    {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String shopName = etShopName.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(shopName)) {
            Toast.makeText(ShangHuActivity.this, "请输入用户名、密码和商店名", Toast.LENGTH_LONG).show();
            return;
        }

        OkHttpClient client = new OkHttpClient(); // 创建http客户端
        String json = String.format("{\n" +
                "   \"userName\":\"%s\",\n" +
                "   \"userPwd\":\"%s\",\n" +
                "   \"shopName\":\"%s\"\n" +
                "}", username, password, shopName);
        String url = "http://192.168.239.136:8888/api/merchant/login"; // 假设商家登录的接口地址是这个
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build(); // 创建http请求

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "登录失败1: " + e.getMessage(), Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "登录失败2: " + response.code(), Toast.LENGTH_SHORT).show());
                    return;
                }
                String responseBody = response.body().string();
                if (responseBody.isEmpty()) {
                    runOnUiThread(() -> Toast.makeText(ShangHuActivity.this, "登录失败: 用户名、密码或商店名错误", Toast.LENGTH_SHORT).show());
                } else {
                    runOnUiThread(() -> {
                        Toast.makeText(ShangHuActivity.this, "登录成功: " + responseBody, Toast.LENGTH_LONG).show();
                        // 跳转到商家主界面





                    });
                }
            }
        });
    }
}
